<?php

namespace System\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Sql;
use System\Model\News;
use Zend\Session\Container;

class NewsTable extends AbstractTableGateway {

    protected $table = "tblnews";
    protected $sql;

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Videos());
        $this->initialize();
        $this->sql = new Sql($this->adapter);
    }

    public function loadnewsmodel($row_per, $start, $key) {
        $batdau = 0;
        $batdau = (($start - 1) * $row_per);
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->where->like("news_Title", '%' . $key . '%');
        $sqlEx->order('news_Id DESC');
        $sqlEx->limit($row_per);
        $sqlEx->offset($batdau);

        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $resultSet = $pst->execute();
        $data = array();
        foreach ($resultSet as $result) {
            $data[] = $result;
        }
        return $data;
    }

    public function countloadnews($key) {
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);

        $sqlEx->where->like("news_Title", '%' . $key . '%');
        $sqlEx->order('news_Id DESC');
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $resultSet = $pst->execute();
        $data = array();
        foreach ($resultSet as $result) {
            $data[] = $result;
        }
        return count($data);
    }

    public function checkurlnews($url) {
        $sqlex = $this->sql->select();
        $sqlex->from($this->table);
        $sqlex->where(array("news_link" => $url));
        $pst = $this->sql->prepareStatementForSqlObject($sqlex);
        $rs = $pst->execute();
        $check = $rs->current();
        if ($check != null) {
            return $url . "-1";
        } else {
            return $url;
        }
    }

    public function addnews(News $objNews) {
        $data = array();
        $data["news_Active"] = $objNews->news_Active;
        $data["news_Content"] = $objNews->news_Content;
        $data["news_Date"] = $objNews->news_Date;
        $data["news_Description"] = $objNews->news_Description;
        $data["news_Title"] = $objNews->news_Title;
        $data["news_imgThumb"] = $objNews->news_imgThumb;
        $data["news_link"] = $objNews->news_link;
        $sqlEx = $this->sql->insert();
        $sqlEx->into($this->table);
        $sqlEx->values($data);
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $rs = $pst->execute();
        if ($rs != null) {
            return true;
        } else {
            return false;
        }
    }

    public function status($newsId, $active) {
        $sqlEx = $this->sql->update();
        $sqlEx->table($this->table);
        $sqlEx->set(array("news_Active" => $active));
        $sqlEx->where(array("news_Id" => $newsId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $pst->execute();
    }

    public function delImg($newsId) {
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->columns(array("news_imgThumb" => "news_imgThumb"));
        $sqlEx->where(array("news_Id" => $newsId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $rs = $pst->execute();
        $data = $rs->current();
        $urlImg = $data["news_imgThumb"];
        $dirpath = str_replace("\\", "/", APPLICATION_PATH . "/public/");
        @unlink($dirpath . $urlImg);
    }

    public function delnews($newsId) {
        $sqlEx = $this->sql->delete();
        $sqlEx->from($this->table);
        $sqlEx->where(array("news_Id" => $newsId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $check = $pst->execute();
        if ($check != null) {
            return true;
        } else {
            return false;
        }
    }

    public function getdataedit($newsId) {
        $sqlEx = $this->sql->select();
        $sqlEx->from($this->table);
        $sqlEx->columns(array(
            "news_Id" => "news_Id",
            "news_Title" => "news_Title",
            "news_link" => "news_link",
            "news_Description" => "news_Description",
            "news_Content" => "news_Content",
            "news_imgThumb" => "news_imgThumb",
        ));
        $sqlEx->where(array("news_Id" => $newsId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $resultSet = $pst->execute();
        $data = $resultSet->current();
        return $data;
    }

    public function updatenews($data, $newsId) {
        $sqlEx = $this->sql->update();
        $sqlEx->table($this->table);
        $sqlEx->set($data);
        $sqlEx->where(array("news_Id" => $newsId));
        $pst = $this->sql->prepareStatementForSqlObject($sqlEx);
        $rs = $pst->execute();
        if ($rs != null) {
            return true;
        } else {
            return false;
        }
    }

}
